
package com.be.beadmin.project.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.project.dto.input.PayInfoDto;
import com.be.beadmin.project.dto.output.PayInfoEx;
import com.be.beadmin.project.entity.Info;
import com.be.beadmin.project.entity.PayInfo;
import com.be.beadmin.project.entity.PayInfo_;
import com.be.beadmin.project.mapper.PayInfoMapper;
import com.be.beadmin.project.mapper.PayInfoMapperEx;
import com.be.beadmin.project.repository.InfoRepository;
import com.be.beadmin.project.repository.PayInfoRepository;
import com.be.beadmin.project.service.PayInfoService;
import com.be.beadmin.service.util.NumberUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zjm
 * @description 服务实现
 * @date 2023-06-14
 **/
@Service
@RequiredArgsConstructor
public class PayInfoServiceImpl implements PayInfoService {

    @Autowired
    private PayInfoRepository payInfoRepository;
    @Autowired
    private PayInfoMapper payInfoMapper;
    @Autowired
    private PayInfoMapperEx payInfoExMapper;

    @Autowired
    private InfoRepository infoRepository;

    @Override
    public Page<PayInfoEx> getPayInfoByPage(PageEntity pageEntity) {
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() - 1,
                pageEntity.getPageSize());
        Page<PayInfo> page = payInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 = JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PayInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if (pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PayInfo_.createTime).as(Date.class)));
                    }
                    return criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<PayInfoEx> page1 = page.map(payInfoExMapper::toEntity);
        return page1;

    }

    @Override
    public void addPayInfo(PayInfoDto payInfoDto) {
        PayInfo result = payInfoRepository.findPayInfoByIdAndDelete(payInfoDto.getInfoId(), 0);
        if (result != null) {
            payInfoDto.setId(result.getId());
        }
        PayInfo payInfo = payInfoMapper.toEntity(payInfoDto);
        payInfoRepository.save(payInfo);
    }

    @Override
    public void updatePayInfo(PayInfoDto payInfoDto) {
        PayInfo payInfo = payInfoRepository.findPayInfoById(payInfoDto.getId());
        // 找到项目，获取到合同金额。
        Info info = infoRepository.findInfoByIdAndDelete(payInfo.getInfoId(), 0);
//        info.getContractAmount()
        payInfo = payInfoMapper.toEntity(payInfoDto);
//        if (payInfo.getAdvancePay() != null) {
//            payInfo.setAdvancePayRate(NumberUtil.roundTwo(payInfo.getAdvancePay() /));
//        }

        payInfoRepository.save(payInfo);
    }


    @Override
    public void delPayInfo(String id) {
        PayInfo payInfo = payInfoRepository.findPayInfoById(id);
        payInfo.setDelete(1);
        payInfoRepository.save(payInfo);
    }

    @Override
    public void delPayInfos(List<String> ids) {
    }

    /**
     * 根据获取项目Id获取付款分页信息
     *
     * @param infoId
     * @return
     */
    @Override
    public PayInfoEx getPayInfoByInfoId(String infoId) {
        PayInfo result = payInfoRepository.findPayInfoByInfoIdAndDelete(infoId, 0);
        if (result == null) {
            PayInfo payInfo = new PayInfo();
            payInfo.setInfoId(infoId);
            result = payInfoRepository.save(payInfo);
//            return new PayInfoEx();
        }
        return payInfoExMapper.toEntity(result);

    }


}